package util

import (
	"bytes"
	"image/png"
	"io"
	"math/rand"
	"strconv"
	"time"

	"github.com/boombuler/barcode"
	"github.com/boombuler/barcode/qr"
)

// GenQRCode ...
func GenQRCode(writer io.Writer, content string) (err error) {
	var qrcode barcode.Barcode
	if qrcode, err = qr.Encode(content, qr.L, qr.Auto); err == nil {
		if qrcode, err = barcode.Scale(qrcode, 300, 300); err == nil {
			err = png.Encode(writer, qrcode)
		}
	}
	return
}

// GenVerifyCode ...
func GenVerifyCode(len int) string {
	var buf bytes.Buffer
	rand.Seed(int64(time.Now().Nanosecond()))
	for i := 0; i < len; i++ {
		buf.WriteString(strconv.Itoa(rand.Intn(10)))
	}
	return buf.String()
}
